package com.panda.cn.service.impl.platform;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panda.cn.config.exception.RestfulException;
import com.panda.cn.entity.platform.PlAppEntity;
import com.panda.cn.mapper.platform.PlAppMapper;
import com.panda.cn.service.platform.PlAppService;
import com.panda.cn.utils.jwt.WebContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 平台应用服务impl
 *
 * @author Yanmh
 * @date 2022/10/20
 */
@Slf4j
@Service
@DS("sys")
public class PlAppServiceImpl implements PlAppService {
    
    @Resource
    private PlAppMapper plAppMapper;
    
    
    /**
     * 页面列表
     *
     * @param params 参数个数
     * @return {@link Page}
     */
    @Override
    public Page pageList(JSONObject params) {
        Long current = params.getLong("current");
        Long size = params.getLong("size");
        log.info("当前页数>>>{}", current);
        log.info("当前页显示数量>>>{}", size);
        Page<PlAppEntity> page = new Page<>(current, size);
        return  plAppMapper.findPage(page, params);
    }
    
    /**
     * 插入
     *
     * @param params 参数个数
     * @return {@link Long}
     */
    @Override
    public Long insert(PlAppEntity params) {
        // 判断是否为空
        if(StrUtil.isEmpty(params.getName()) || StrUtil.isEmpty(params.getMessage()))
            throw new RestfulException("名称或描述不能为空");
        // 查看是不是存在相同的编码和名称
        LambdaQueryWrapper<PlAppEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlAppEntity::getName, params.getName());
        Integer count = plAppMapper.selectCount(queryWrapper);
        if(count>0)
            throw new RestfulException("名称存在相同");
        // 创建接口uuid
        String code = IdUtil.randomUUID();
        params.setCode(code);
        // 当前时间
        DateTime nowDateTime = DateUtil.parse(DateUtil.now());
        params.setCreateTime(nowDateTime);
        params.setUpdateTime(nowDateTime);
        // 添加
        params.setUserCode(WebContextUtil.getUserInfo().getCode());
        plAppMapper.insert(params);
        return params.getId();
    }
    
    /**
     * 更新
     *
     * @param params 参数个数
     * @return {@link Long}
     */
    @Override
    public Long update(PlAppEntity params) {
        // 判断是否为空
        if (ObjUtil.isEmpty(params.getId()))
            throw new RestfulException("唯一编码缺失");
        if(StrUtil.isEmpty(params.getName()) || StrUtil.isEmpty(params.getMessage()))
            throw new RestfulException("名称或描述不能为空");
        // 查看是不是存在相同的编码和名称
        LambdaQueryWrapper<PlAppEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(PlAppEntity::getId, params.getId())
                .and(i -> i.eq(PlAppEntity::getName, params.getName()));
    
        Integer count = plAppMapper.selectCount(queryWrapper);
        if(count>0)
            throw new RestfulException("名称存在相同");
    
        // 当前时间
        DateTime nowDateTime = DateUtil.parse(DateUtil.now());
        params.setUpdateTime(nowDateTime);
        // 添加
        plAppMapper.updateById(params);
    
        return params.getId();
    }
    
    /**
     * 删除
     *
     * @param params 参数个数
     * @return {@link Long}
     */
    @Override
    public Long remove(List<Long> params) {
        // 验证
        if (ObjUtil.isNull(params) || params.size() == 0)
            throw new RestfulException("唯一编码缺失");
        //  更新的编码
        LambdaQueryWrapper<PlAppEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PlAppEntity::getId, params);
    
        // 更新的数据
        PlAppEntity entity = new PlAppEntity();
        entity.setIsDelete("Y");
    
        int update = plAppMapper.update(entity, queryWrapper);
        return Long.valueOf(update);
    }
}
